package cn.jksoft.businessclient.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jksoft.businessclient.R;
import cn.jksoft.businessclient.bean.OnlineOrderListEntity;

/**
 * Created by RogersHuang on 16/4/7.
 */
public class OnlineOrderAdapter extends RecyclerView.Adapter<OnlineOrderAdapter.ItemViewHolder> {
    private Context context;
    private LayoutInflater inflater;
    private List<OnlineOrderListEntity.ObjListBean> dataList;
    private View.OnClickListener itemClickListener;
    private View.OnClickListener buttonClickListenner;

    public void setItemClickListener(View.OnClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setButtonClickListenner(View.OnClickListener buttonClickListenner) {
        this.buttonClickListenner = buttonClickListenner;
    }

    public OnlineOrderAdapter(Context context , List<OnlineOrderListEntity.ObjListBean> list) {
        this.context = context;
        this.dataList = list;
        inflater = LayoutInflater.from(context);
    }

    public void addDataList(List<OnlineOrderListEntity.ObjListBean> list){
        this.dataList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.item_online_order, viewGroup, false);
        ItemViewHolder itemViewHolder = new ItemViewHolder(view);
        view.setOnClickListener(itemClickListener);
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(final ItemViewHolder itemViewHolder, int i) {
        OnlineOrderListEntity.ObjListBean dataBean = dataList.get(i);
        final String custPhone = (String)dataBean.getCustomerPhone();
        itemViewHolder.tvServiceName.setText(dataBean.getOrderServiceName());
        itemViewHolder.tvPayRecommend.setText("用户需支付现金"+dataBean.getDGrandTotal());
        itemViewHolder.tvOrderNum.setText("订单编号:"+dataBean.getSvrCode());
        itemViewHolder.tvCarNum.setText("车牌号:"+dataBean.getCarPlateNum());
        itemViewHolder.tvCarInfo.setText("车辆信息:"+dataBean.getCarModelInfo());
        itemViewHolder.tvOrderTime.setText("下单时间:"+dataBean.getCreatedDate());
        itemViewHolder.btnOperOrder.setTag(i);
        itemViewHolder.btnOperOrder.setOnClickListener(buttonClickListenner);
        if(dataBean.getOrderState().equals("10002")){
            itemViewHolder.ivOrderStatus.setImageResource(R.mipmap.queren);
            itemViewHolder.btnOperOrder.setText("确认");
        }else if(dataBean.getOrderState().equals("10004")){
            itemViewHolder.ivOrderStatus.setImageResource(R.mipmap.orderservice);
            itemViewHolder.btnOperOrder.setText("完成服务");
        }else{
            itemViewHolder.ivOrderStatus.setImageResource(R.mipmap.orderfinish);
            itemViewHolder.btnOperOrder.setVisibility(View.GONE);
        }

        itemViewHolder.btnCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri localUri = Uri.parse("tel:"+ custPhone);
                Intent call = new Intent();
                call.setAction(Intent.ACTION_DIAL);
                call.setData(localUri);
                context.startActivity(call);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_service_name)
        TextView tvServiceName;
        @Bind(R.id.iv_online_order_status)
        ImageView ivOrderStatus;
        @Bind(R.id.tv_pay_recommend)
        TextView tvPayRecommend;
        @Bind(R.id.tv_order_number)
        TextView tvOrderNum;
        @Bind(R.id.tv_car_number)
        TextView tvCarNum;
        @Bind(R.id.tv_car_info)
        TextView tvCarInfo;
        @Bind(R.id.tv_order_time)
        TextView tvOrderTime;
        @Bind(R.id.btn_call)
        ImageButton btnCall;
        @Bind(R.id.btn_operate_order)
        AppCompatButton btnOperOrder;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
